#!/bin/bash
#
#2021年9月16日 11点19分
#Linux ubuntu 5.11.0-34-generic #36~20.04.1-Ubuntu SMP Fri Aug 27 08:06:32 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
#Zabbix 5.4

apt -y install wget 
#配置软件仓库
wget -P /opt/ https://repo.zabbix.com/zabbix/5.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.4-1+ubuntu20.04_all.deb
dpkg -i /opt/zabbix-release_5.4-1+ubuntu20.04_all.deb

#及时更新仓库
apt update
apt -y install zabbix-server-mysql zabbix-frontend-php zabbix-apache-conf zabbix-sql-scripts zabbix-agent
apt update
apt -y install mysql-server

systemctl restart mysql

#Ubuntu默认没有expect
apt -y install expect
expect -c "
spawn /usr/bin/mysql_secure_installation
expect \"Press y|Y for Yes, any other key for No:\"
send \"y\r\"
expect \"Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:\"
send \"0\r\"
expect \"New password:\"
send \"htzy0000\r\"
expect \"Re-enter new password:\"
send \"htzy0000\r\"
expect \"Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :\"
send \"y\r\"
expect \"Remove anonymous users?\"
send \"y\r\"
expect \"Disallow root login remotely?\"
send \"n\r\"
expect \"Remove test database and access to it?\"
send \"y\r\"
expect \"Reload privilege tables now?\"
send \"y\r\"
expect eof
"
#支持单引号语句
mysql -uroot -phtzy0000 -e "create database zabbix character set utf8 collate utf8_bin;"
mysql -uroot -phtzy0000 -e "create user 'zabbix'@'localhost' identified by 'htzy0000';"
mysql -uroot -phtzy0000 -e  "grant all privileges on *.* to zabbix@localhost;"

#sql文件没有指定数据库
zcat /usr/share/doc/zabbix-sql-scripts/mysql/create.sql.gz | mysql -uzabbix -phtzy0000 -Dzabbix

#配置文件数据库密码
sed -i 's/# DBPassword=/DBPassword=htzy0000/g' /etc/zabbix/zabbix_server.conf

systemctl restart zabbix-server zabbix-agent apache2
systemctl enable zabbix-server zabbix-agent apache2 mysql

#安装中文语言包
apt -y install language-pack-zh-han*